package com.fashion.web.actions.management.news;

import com.fashion.model.NewsRecord;
import com.fashion.service.NewsRecordService;
import com.fashion.utils.GeneralResult;
import com.fashion.utils.StringUtils;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.logicalcobwebs.proxool.ProxoolConstants;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: gnyk
 * Date: 13-9-4
 */
@Scope("prototype")
@Controller
@Namespace("/management/news")
@ParentPackage("json-default")
public class NewsRecordAction extends ActionSupport {
    private GeneralResult generalResult;
    private NewsRecordService newsRecordService;
    private String creatorId;
    private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM");

    @Action(value = "increaseViewTime", results = {@Result(name = SUCCESS, type = "json", params = {"root", "generalResult"})})
    public String increaseViewTime() throws Exception {
        if(StringUtils.isNullOrEmpty(creatorId)){
            return SUCCESS;
        }

        generalResult = new GeneralResult();
        Date now = new Date();
        String currentDate = SIMPLE_DATE_FORMAT.format(now);

        NewsRecord newsRecord = searchNewsRecord(currentDate);
        if (null != newsRecord) {
            newsRecord.setAccessNumber(newsRecord.getAccessNumber() + 1);
        } else {
            newsRecord = new NewsRecord();
            newsRecord.setAccessNumber(1);
            newsRecord.setId(StringUtils.createGuid());
            newsRecord.setAccessDate(SIMPLE_DATE_FORMAT.parse(currentDate));
            newsRecord.setCreatorId(creatorId);
        }

        newsRecordService.saveOrUpdate(newsRecord);

        return SUCCESS;
    }

    protected NewsRecord searchNewsRecord(String currentDate) {
        List<NewsRecord> newsRecordList = newsRecordService.searchRecordByCreatorId(creatorId);

        for (NewsRecord newsRecord : newsRecordList) {
            Date date = newsRecord.getAccessDate();
            if (currentDate.equals(SIMPLE_DATE_FORMAT.format(date))) {
                return newsRecord;
            }
        }
        return null;
    }

    public GeneralResult getGeneralResult() {
        return generalResult;
    }

    public void setGeneralResult(GeneralResult generalResult) {
        this.generalResult = generalResult;
    }

    @Resource(name = "newsRecordService")
    public void setNewsRecordService(NewsRecordService newsRecordService) {
        this.newsRecordService = newsRecordService;
    }

    public String getCreatorId() {
        return creatorId;
    }

    public void setCreatorId(String creatorId) {
        this.creatorId = creatorId;
    }
}
